<?php

/**
 * Item form.
 *
 * @package    form
 * @subpackage Item
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class ItemForm extends BaseItemForm
{
	public function configure()
	{
		parent::configure();
		
		$this->widgetSchema['id_artigo_conexao'] = new sfWidgetFormInputHidden();
		
		$this->widgetSchema['nm_item'] = new sfWidgetFormInput(
			array(),
			array('class' => 'required', 'size' => '100', 'maxlength' => '50')
		);
		
		$options = array('N'=>'Não', 'S'=>'Sim');
		$this->widgetSchema['st_item'] = new sfWidgetFormChoice(
			array('choices'=>$options, 'expanded'=>true, 'multiple'=>false)
		);
		
		$this->widgetSchema['is_top'] = new sfWidgetFormChoice(
			array('choices'=>$options, 'expanded'=>true, 'multiple'=>false)
		);
		
		$this->widgetSchema['id_tipo'] = new sfWidgetFormDoctrineChoice(
			array('model' => 'Tipo', 'add_empty' => '[-- Selecione --]')
		);
		
		if($this->getObject()->getIdTipo() == Tipo::ARTIGOS || $this->getObject()->getIdTipo() == Tipo::NOTICIAS)
		{
			$options = array(''=>'-- Selecione um Tipo --', '5'=>'Artigos', '4'=>'Notícias');
			$this->widgetSchema['id_tipo'] = new sfWidgetFormChoice(
				array('choices'=>$options, 'expanded'=>false, 'multiple'=>false)
				,
	        	array(
	        		'style' => 'min-width: 150px;'
	        	)
	        );
		}
		
		$this->widgetSchema['id_item_pai'] = new sfWidgetFormDoctrineChoice(
			array('model' => 'Item', 'add_empty' => '[-- Selecione --]')
		);
		
		$this->widgetSchema['id_usuario'] = new sfWidgetFormDoctrineChoice(
			array('model' => 'DadosUsuario', 'add_empty' => '[-- Selecione --]')
		);
		
		$this->widgetSchema['ds_item'] = new sfWidgetFormTextarea(
			array(),
			array('id' => 'tinymce')
        );
        
        $this->widgetSchema['ds_resumo_item'] = new sfWidgetFormTextarea(
        	array(),
        	array('cols'=>70,'rows'=>5)
        );
		
		$this->widgetSchema['ds_tags'] = new sfWidgetFormInput(
			array(),
			array('class' => 'required', 'size' => '100', 'maxlength' => '200')
		);
		
		$this->widgetSchema['ds_url'] = new sfWidgetFormInput(
			array(),
			array('class' => 'required url', 'size' => '100', 'maxlength' => '255')
		);
		
		if($this->getObject()->getIdTipo() == Tipo::PRODUTOS)
		{
			$this->widgetSchema['categoria_list'] = new sfWidgetFormDoctrineChoiceMany(
	        	array(
	        		'model' => 'Categoria',
	        		'query' => CategoriaTable::carregarCombo(Tipo::PRODUTOS)
	        	),
	        	array(
	        		'style' => 'min-width: 150px;'
	        	)
	        );
		}
		
		/*
       	 * Formulário embutido de mídia
       	 */
      	$form = new MidiaItemForm(null, array(
			'item' => $this->getObject(),//nome do objeto
			'size'    => 10//quantidade de registros
		));
        $this->embedForm('midia', $form);
		
		$this->widgetSchema->setLabels(array(
			'id_item'        => 'ID#',
	      	'nm_item'        => 'Nome / Titulo',
	     	'st_item'        => 'Publicar',
	      	'is_top'         => 'Topo',
	      	'id_tipo'        => 'Tipo',
	      	'id_item_pai'    => 'Item Pai',
	      	'id_usuario'     => 'Usuário',
	      	'ds_item'        => 'Conteudo',
	      	'ds_resumo_item' => 'Resumo',
	      	'ds_tags'        => 'Tags',
	      	'slug'           => 'Slug',
			'ds_url'         => 'Url',
			'created_at'     => 'Criado em',
			'updated_at'     => 'Atualizado em',
	      	'categoria_list' => 'Categorias'
		));
	}
	
	public function saveEmbeddedForms($con = null, $forms = null)
	{
		if (null === $forms)
		{
			$midia = $this->getValue('midia');
			$itemcategoria  = $this->getValue('itemcategoria');
			$forms = $this->embeddedForms;
			foreach ($this->embeddedForms['midia'] as $name => $form)
			{
				if (!isset($midia[$name]))
				unset($forms['midia'][$name]);
			}
		}
		parent::saveEmbeddedForms($con, $forms);
		 
		//Criando as Thumbs
		foreach($this->getObject()->getMidia() as $midia)
		{
			$thumb   = new Midia();
			$thumb->createThumb($midia);
		}

		return;
	}
}